// pages/member/card/index.js
import api from "../../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "date",
        names: ["expire_time_form", "expire_start_date", "expire_end_date"],
        type: "pickerDate",
        label: "到期时间",
      },
      {
        name: "card_form",
        type: "picker",
        label: "卡类型",
      },
      {
        name: "card_status",
        type: "picker",
        label: "状态",
        span: 8,
      },
      {
        name: "duration_form",
        type: "picker",
        label: "时长",
        span: 8,
      },
    ],
    searchNum: [
      {
        label: "金额(元)",
        num: "0.00",
        show: true,
      },
      {
        label: "卡数",
        num: 0,
      },
    ],
    sExplain: {
      title: "搜索说明",
      content: "时间可以选择，对会员卡到期时间、类型、状态、时长进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对用户会员卡消费金额，卡数进行统计",
    },
    searchParam: {},
    //#endregion
    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion

    userInfo: {},
    show: false,
    images: [],

    colors: {
      use: "#73F0EA",
      nulli: "#47CC88",
      expire: "#EE8E3A",
      cancel: "#999999",
      refund: "#F37364",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum, id } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreVipCard({
        ...searchParam,
        page,
        user_id: id,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page === 1) {
          searchNum[0].num = Number(data.entirety.cost).toFixed(2);
          searchNum[1].num = data.entirety.card_num;

          data.top_info.user_info.tagVal = data.top_info.is_vip;
          data.top_info.user_info.tagStr = data.top_info.vip_str;
          this.setData({
            searchNum,
            userInfo: data.top_info,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  previewImage(e) {
    wx.previewImage({
      urls: e.currentTarget.dataset.url.split(";"),
    });
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  openPopup(e) {
    let item = e.currentTarget.dataset.item;
    let images = [item.entering_face, item.face];
    this.setData({
      images,
      show: true,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
